/*
 *  Copyright 2007-2008 by Matthias Buch-Kromann <mbk.isv@cbs.dk>.
 *  *
 *  This file is part of the Open-source Dependency Toolkit (OSDT),
 *  see http://code.google.com/p/open-source-dependency-toolkit.
 *  
 *  The OSDT is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as 
 *  published by the Free Software Foundation, either version 3 of 
 *  the License, or (at your option) any later version.
 * 
 *  The OSDT is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public 
 *  License along with the OSDT in the files COPYING and COPYING.LESSER.
 *  If not, see http://www.gnu.org/licenses.
 */

package org.osdtsystem.graphs.io;

import java.util.regex.Matcher;
import org.osdtsystem.exceptions.FeatureNotFoundException;
import org.osdtsystem.exceptions.LayerNotFoundException;
import org.osdtsystem.exceptions.LocationNotFoundException;
import org.osdtsystem.exceptions.NodeNotFoundException;
import org.osdtsystem.exceptions.OsdtSyntaxException;
import org.osdtsystem.graphs.Graph;
import org.osdtsystem.values.EdgeID;
import org.osdtsystem.values.Layer;
import org.osdtsystem.values.StringFeature;

/**
 *
 * @author Matthias Buch-Kromann <mbk.isv@cbs.dk>
 */
public class EdgeDeclaration {
   Layer layer;
    Integer in, out;
    String featvals;

    EdgeDeclaration(Layer layer, String sin, String sout, String featvals) 
            throws OsdtSyntaxException {
        this.layer = layer;
        this.in = Integer.valueOf(sin);
        this.out = Integer.valueOf(sout);
        this.featvals = featvals;

        // Check that layer exists
        if (layer == null)
            throw new OsdtSyntaxException(
                "Invalid layer when reading edge in OSDT encoding: " 
                    + toString());  
    }

    void addEdge(Graph g, GraphIDs ids, ObjectDictionary dictionary) 
            throws OsdtSyntaxException {
        try {
            EdgeID e = g.addEdge(ids.getNode(in), ids.getNode(out), layer);
            Matcher m = OsdtHelper.osdtFeatureValuePattern.matcher(featvals);
            while (m.find())
                g.setFeatureValue(e, 
                        dictionary.compileFeature(OsdtHelper.unquote(m.group(1))),
                        dictionary.compile(new StringFeature(OsdtHelper.unquote(m.group(2)))));
        } catch (LayerNotFoundException ex) {
            throw new OsdtSyntaxException(
                "Unexpected invalid layer when reading edge in OSDT encoding: "
                    + toString(), ex);
        } catch (NodeNotFoundException ex) {
            throw new OsdtSyntaxException(
                "Invalid in- or out-node when reading edge in OSDT encoding: "
                    + toString(), ex);
        } catch (LocationNotFoundException ex) {
            throw new OsdtSyntaxException(
                "Unexpected edge creation error when reading edge in OSDT encoding: "
                    + toString(), ex);
        } catch (FeatureNotFoundException ex) {
            throw new OsdtSyntaxException(
                "Invalid feature when reading edge in OSDT encoding: "
                    + toString(), ex);
        }
    }

    @Override
    public String toString() {
        return "EDGE " + in + "<-" + out + " " + featvals;
    }
}